import dayjs from 'dayjs';
import weekday from 'dayjs/plugin/weekday';
// 注册插件
dayjs.extend(weekday);

/**
 * 获取指定年份所有周的信息（限定在指定年内）
 * @param {number} targetYear - 指定年份（如 2025）
 * @returns {Array} 每周信息数组，格式：[{ week: 周数, start: 'MM-DD', end: 'MM-DD' }, ...]
 */
interface Week {
	week: number;
	start: string;
	end: string;
}
export function getCurrWeek(year: number): number {
	let currDate = dayjs();
	let currDay = currDate.date();
	let currMonth = currDate.month() + 1;
	let specifiedDate = `${year}-${currMonth}-${currDay}`;
	return dayjs(specifiedDate).week();
}
export function getYearWeeks(year: number) {
	let week = 1;
	let weeks: Week[] = [];
	let endDay = dayjs(`${year}-12-31`);
	let startDay = dayjs(`${year}-01-01`);
	let firstWeekMonday = startDay.weekday(1);
	let currWeekStart = firstWeekMonday.year() < year ? startDay : firstWeekMonday;
	// 循环遍历所有周，直到超过当年最后一天
	while (currWeekStart.isBefore(endDay) || currWeekStart.isSame(startDay)) {
		// 计算本周结束日（周日），若超过年末则取12月31日
		let currWeekEnd = currWeekStart.weekday(7); // 7=周日
		if (currWeekEnd.year() > year) {
			currWeekEnd = endDay;
		}
		weeks.push({ week, start: currWeekStart.format('MM.DD'), end: currWeekEnd.format('MM.DD') });
		// 下一周的周一
		currWeekStart = currWeekStart.add(1, 'week');
		week++;
	}
	return weeks;
}
